
/*
 * =====================================================================================
 *       Filename:  TTSEngineMgr.h
 *
 *    Description:
 *
 *
 *        Version:  1.0
 *        Created:  03/06/19 15:59:09
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  liushuyong (), shuyong1999@163.com
 *        Company:  shuyong
 *
 * =====================================================================================
 */

#ifndef  TTSEngineMgr_H_
#define  TTSEngineMgr_H_

#include "CommHead.h"
#include "RPCContext.h"
#include "HttpParam.h"
#include "Lock.h"

#include "ttscei.h"

using std::shared_ptr;
using std::string;
using std::atomic;

class RequestSession;
class TTSEngineMgr;

//typedef aispeech::tts::TASK_PARAMETER TTSEngineParam;   //TTS Engine中参数
//typedef shared_ptr < aispeech::tts::TtsServiceTask > TTSEngineTaskSPtr;
//typedef shared_ptr < aispeech::tts::TtsService > TTSEngineService;

typedef struct {
    int voice_type; // wav, pcm, opus
    int sample_rate;    // [4000, 24000]
    float volume;  // [0.1, 10.0]
    float speed;   // [0.5, 2.0]
    int timestamp_type; // 0 disable, 1 phone's timestamp, 2 syllable's timestamp
    int model_index;    // [0, 1], currently we have 2 timbre
    bool return_all;    // synthesis all text and return all data
    int text_type; // wether the text is ssml
} TTSEngineParam;

typedef shared_ptr < TTSEngineMgr > TTSEngineMgrSPtr;

//typedef aispeech::tts::TASK_PARAMETER TTSEngineParam;

class TTSEngineMgr: public brpc::SharedObject {
  public:
    TTSEngineMgr();
    virtual ~ TTSEngineMgr();

    void startAsyncTask(RPCContextSPtr& rpcCtx);
    void CreateTTSTask(TTSEngineParam* param);
    bool readCache(RPCContextSPtr rpcCtx, bool isFist);
    static bool loadResouce(const string& modelPath);
    static void* firstPackProc(void* param);
    atomic < bool > mIsFinish;  // query finished;
    atomic < int32_t > mErrNo;

    //string mReqID;
    //int mSeqID;
    //string mTxt;

    RWLock mParamLock;  //参数锁
    bthread_t _bthread_handle;
  private:
    static void* ttsEngineProc(void* param);

  private:
    RPCContextSPtr mspRPCContext;   //只在第一包的时候使用
    bthread_t mBthreadID;   //创建线程
    //static TTSEngineService mgpTTSEngine;   //TTS Engine的句柄
    //TTSEngineTaskSPtr mspTTSEngineTask;
    void* tts_task;

};

#endif
